package com.bifrost.admin.controller;

import com.bifrost.annotation.Authorization;
import com.bifrost.annotation.CurrentAdmin;
import com.bifrost.common.model.ResultModel;
import com.bifrost.entity.Category;
import com.bifrost.entity.User;
import com.bifrost.model.CategoryModel;
import com.bifrost.service.AdminUserService;
import com.bifrost.service.CategoryService;
import com.bifrost.validator.group.IsAdd;
import com.bifrost.validator.group.IsUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @com.bifrost.admin.controller
 * @wechat-mall
 * @lee Create Class on 2018/4/9
 */
@RestController
@RequestMapping("/admin/category")
@Api(value = "/后台分类相关接口")
@Validated
public class AdminCategoryController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AdminUserService userService;

    @PostMapping("/init")
    public ResultModel sapCategoryInit() {
        categoryService.sapCategoryInit();
        return ResultModel.ok();
    }

    @PostMapping("/sync")
    public ResultModel sapCategorySync() {
        categoryService.sapCategorySync();
        return ResultModel.ok();
    }

    @ApiOperation(value = "树状结构返回品类")
    @GetMapping("/tree")
    public ResultModel categories() {
        List<Category> categories = categoryService.findAllCategory();
        if (CollectionUtils.isEmpty(categories)) {
            return ResultModel.error("没有数据");
        }
        return ResultModel.ok().put("categories", categories);
    }

    @ApiOperation(value = "展示商品分类", notes = "根据分类id 展示商品分类详情")
    @GetMapping("/show")
    @ApiImplicitParams({@ApiImplicitParam(name = "categoryId", value = "商品分类ID", required = true, dataType = "Long", paramType = "query")})
    public ResultModel getCategory(@RequestParam @NotNull(message = "分类不能为空") Long categoryId) {
         return ResultModel.ok().put("result", categoryService.detail(categoryId));

    }


    @ApiOperation(value = "添加商品分类", notes = "添加商品分类: 不能带categoryId 参数")
    @PostMapping("/add")
    @ApiImplicitParams({@ApiImplicitParam(name = "categoryModel", value = "商品分类信息", required = true, dataType = "CategoryModel")})
    public ResultModel addCategory(@CurrentAdmin Long userId , @RequestBody @Validated(IsAdd.class) CategoryModel categoryModel) {
        categoryService.save(userService.findById(userId) ,categoryModel);
        return ResultModel.ok("分类保存成功");
    }

    @ApiOperation(value = "修改商品分类")
    @PostMapping("/update")
    @ApiImplicitParams({@ApiImplicitParam(name = "categoryModel", value = "商品分类信息", required = true, dataType = "CategoryModel")})
    @Authorization
    public ResultModel updateCategory(@CurrentAdmin Long userId ,@RequestBody @Validated({IsUpdate.class}) CategoryModel categoryModel) {
        categoryService.save(userService.findById(userId) , categoryModel);
        return ResultModel.ok("分类修改成功");
    }

    @ApiOperation(value = "删除商品分类")
    @GetMapping("/delete")
    @ApiImplicitParams({@ApiImplicitParam(name = "categoryId", value = "商品分类ID", required = true, dataType = "Long", paramType = "query")})
    public ResultModel deleteCategory(@CurrentAdmin Long userId ,@RequestParam @NotNull(message = "分类不能为空") Long categoryId) {
        categoryService.delete(userService.findById(userId) ,categoryId);
        return ResultModel.ok("分类删除成功");
    }

}
